package com.example.demo.dao.impl;


import com.example.demo.dao.RenDao;
import com.example.demo.model.Ren;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;


@Repository
@Transactional(rollbackFor = Exception.class)
public class RenDaoImpl  implements RenDao {

    @Autowired
    HibernateTemplate hibernateTemplate;


    @Override
	public void deleteBean(Ren bean) {
       hibernateTemplate.delete(bean);

    }

    @Override
	public void insertBean(Ren bean) {
       hibernateTemplate.save(bean);

    }

    @Override
	@SuppressWarnings("unchecked")
    public Ren selectBean(String where) {
        List<Ren> list = (List<Ren>)hibernateTemplate.find("from Ren " + where);
        if (list.size() == 0) {
            return null;
        }
        return list.get(0);
    }

    @Override
	public int selectBeanCount(String where) {
        long count = (Long)hibernateTemplate.find("select count(*) from Ren " + where).get(0);
        return (int) count;
    }

    @Override
	@SuppressWarnings("unchecked")
    public List<Ren> selectBeanList(final int start, final int limit, final String where) {
        return (List<Ren>)hibernateTemplate.executeWithNativeSession((HibernateCallback) session -> {
            List<Ren> list = session.createQuery("from Ren " + where)
                    .setFirstResult(start)
                    .setMaxResults(limit)
                    .list();
            return list;
        });
    }

    @Override
	public void updateBean(Ren bean) {
       hibernateTemplate.update(bean);

    }


}
